package com.neteast.androidclient.newscenter.domain;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class BootStrapResponse {
	/** 通信协议  */
	public int protocolVersion;
	/** 报文id */
	public int msgId;
	/** 推送服务器ip*/
	public String serverIp;
	/** 推送服务器端口*/
	public short serverPort;
	/** 心跳间隔*/
	public int keepaliveTime;
	/** 保留字段长度*/
	public int reserveLen;
	/** 保留数据*/
	public byte[] reserve;
	
	private ByteBuffer byteBuffer;
	
	public BootStrapResponse(ByteBuffer byteBuffer) {
		this.byteBuffer=byteBuffer;
		init();
	}
	
	
	private void init() {
		protocolVersion=byteBuffer.getInt();
		msgId=byteBuffer.getInt();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<4;i++){
			sb.append(byteBuffer.get()&0xff).append(".");
		}
		sb.deleteCharAt(sb.length()-1);
		serverIp=sb.toString();
		serverPort=byteBuffer.getShort();
		keepaliveTime=byteBuffer.getInt();
		reserveLen=byteBuffer.getInt();
		if (reserveLen>0) {
			reserve=new byte[reserveLen];
			for (int i = 0; i < reserveLen; i++) {
				reserve[i]=byteBuffer.get();
			}
		}
	}
	@Override
	public String toString() {
		if (reserve!=null) {
			return "BootStrapResponse [protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", serverIp=" + serverIp
					+ ", serverPort=" + serverPort + ", keepaliveTime="
					+ keepaliveTime + ", reserveLen=" + reserveLen + ", reserve="
					+ Arrays.toString(reserve) + "]";
		}else {
			return "BootStrapResponse [protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", serverIp=" + serverIp
					+ ", serverPort=" + serverPort + ", keepaliveTime="
					+ keepaliveTime + ", reserveLen=" + reserveLen+"]";
		}
	}
}
